package es.udc.fic.hamNeggs.dao;

import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.manager.UserStoriesManager;
import es.udc.fic.hamNeggs.pojo.Sprint;
import es.udc.fic.hamNeggs.pojo.UserStories;
import es.udc.fic.hamNeggs.pojo.UserStories.State;

import java.sql.Date;
import java.util.ArrayList;

public class InputSprint {
    private String            id;

    private String            name;

    private String            dateStart;

    private String            dateEnd;

    private String            project;

    private int               spleft;

    private ArrayList<String> userStories = new ArrayList<String>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDateStart() {
        return dateStart;
    }

    public void setDateStart(String dateStart) {
        this.dateStart = dateStart;
    }

    public String getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(String dateEnd) {
        this.dateEnd = dateEnd;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public ArrayList<String> getUserStories() {
        return userStories;
    }

    public void setUserStories(ArrayList<String> userStories) {
        this.userStories = userStories;
    }

    public int getUserStoriesSize() {
        return userStories.size();
    }

    public int getUserStoriesDone() {
        int count = 0;

        UserStoriesManager ustmng = new UserStoriesManager();

        for (String userStory : userStories) {
            UserStories ust = ustmng.getUserStories(new Long(userStory));
            if (ust.getState().getString().equalsIgnoreCase("Validated"))
                count++;
        }

        ustmng.close();

        return count;
    }

    public int getStoryPointsDone() {
        int count = 0;

        UserStoriesManager ustmng = new UserStoriesManager();

        for (String userStory : userStories) {
            UserStories ust = ustmng.getUserStories(new Long(userStory));
            if (ust.getState().compareTo(State.Finalized) == 0 || ust.getState().compareTo(State.Validated) == 0)
                count += ust.getRealstoryPoints();
        }

        ustmng.close();

        return count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Sprint toSprint() {
        Sprint sprint = new Sprint();
        ProjectManager prjmng = new ProjectManager();
        UserStoriesManager ustmng = new UserStoriesManager();
        UserManager usrmng = new UserManager();

        if (id != null && id != "")
            sprint.setId(new Long(id));
        if (name != null && name != "")
            sprint.setName(name);
        if (dateStart != null && dateStart != "")
            sprint.setDateStart(Date.valueOf(dateStart));
        if (dateEnd != null && dateEnd != "")
            sprint.setDateEnd(Date.valueOf(dateEnd));
        if (project != null && project != "")
            sprint.setProject(prjmng.getProject(new Long(project)));
        if (userStories != null)
            for (String ust : userStories)
                sprint.setOneProposedTask(ustmng.getUserStories(new Long(ust)));

        usrmng.close();
        prjmng.close();
        ustmng.close();
        return sprint;
    }

    public void setSprint(Sprint sprint) {
        if (sprint.getId() != null)
            id = sprint.getId().toString();
        if (sprint.getName() != null)
            name = sprint.getName();
        if (sprint.getDateStart() != null)
            dateStart = sprint.getDateStart().toString();
        if (sprint.getDateEnd() != null)
            dateEnd = sprint.getDateEnd().toString();
        if (sprint.getProject() != null)
            project = sprint.getProject().getId().toString();
        if (sprint.getProposedTasks() != null && !sprint.getProposedTasks().isEmpty())
            for (UserStories ust : sprint.getProposedTasks())
                userStories.add(ust.getId().toString());

    }

    public int getSpleft() {
        return spleft;
    }

    public void setSpleft(final int spleft) {
        this.spleft = spleft;
    }

}
